define(['app'], function(app) {
    app.controller('MockTokenController', function($rootScope, $http, $uibModalInstance, xUtil, toastr) {
        var self = this;

        self.token = '';

        self.init = function() {
            self.token = $rootScope.sessionUser.token;
            // Copy token
            xUtil.clipboard.copy('#copy-token', function() {
                return self.token;
            });
        }

        self.refresh = function() {
            $http.post('/mock/token/refresh').then(function(response) {
                if (response.data == 'locked') {
                    toastr.warning('已有用户在编辑，请稍候再试！');
                } else if (response.data == 'email') {
                    toastr.error('邮箱不存在！');
                } else if (response.status == 200) {
                    self.token = response.data.token;
                    toastr.success('接口令牌刷已刷新！');
                    // refresh session token
                    $rootScope.sessionUser.token = response.data.token;
                    $cookies.putObject('session_user', $rootScope.sessionUser);
                }
            });
        }

        self.close = function() {
            $uibModalInstance.close();
        }
    });
});
